package com.screenworker.compactmvc.extensions.flex.soapservice
{
	import com.screenworker.compactmvc.extensions.flex.interfaces.ISoapService;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.InvokeEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	import mx.rpc.xml.SchemaTypeRegistry;
	
	public class SoapService extends Model implements IModel, ISoapService
	{
		public static var NAME:String = 'SoapService';
		
		public var service:WebService;
		
		public function SoapService(name:String, controller:Controller)
		{
			super(name, controller);
			
			service = new WebService();   
			service.addEventListener(FaultEvent.FAULT, faultHandler, false, 0, false); 
			service.addEventListener(ResultEvent.RESULT, resultHandler, false, 0, false);
			service.addEventListener(InvokeEvent.INVOKE, invokeHandler, false, 0, false);
			
			//service.loadWSDL(ModelLocator.instance.baseWsUrl + "/" + serviceName + "?wsdl");
		}
		
		override final public function removeHandler():void
		{
			service.disconnect();
			service.addEventListener(FaultEvent.FAULT, faultHandler); 
			service.removeEventListener(ResultEvent.RESULT, resultHandler);
			service.removeEventListener(InvokeEvent.INVOKE, invokeHandler);
			service = null;
		}
		
		public function faultHandler(fault:FaultEvent):void
		{
			
		}
		
		public function resultHandler(result:ResultEvent):void
		{
			
		}
		
		public function invokeHandler(invoke:InvokeEvent):void
		{
			
		}
		
		public function setupTypes():void  
		{  
			/*
			SchemaTypeRegistry.getInstance().registerCollectionClass(new QName("http://model.mynamespace","ArrayOfCountry"),myflex.model.ArrayOfCountry);  
			SchemaTypeRegistry.getInstance().registerClass(new QName("http://model.mynamespace","Country"),myflex.model.Country);  
			...  
			...  */
		}  
		
		/**
		 * returns a given mockup object 
		 * @param mockUp
		 * 
		 */			
		protected final function mockup(mockUp:Object):void
		{ 	
			resultHandler(new ResultEvent(ResultEvent.RESULT,false, false, mockUp));
		}
	}
}